<?php
// --- 调试
// 页面输出 - behero
function log_page($data) {
	if ( is_array($data) || is_object($data) ) {
		echo "<pre>";
		print_r($data);
		echo "</pre>";
	} else {
		echo $data;
	}
}
function log_page_sql() {
	log_page(db()->getlastsql());
}
// 文件输出 - behero
function log_file($data, $desc="", $file="") {
	if ( empty($file) ) $file = date("YmdHi") . ".log";
	$file = ROOT_PATH . "bee/log/" . $file;

	if ( is_array($data) || is_object($data) ) {
		error_log($desc . (!empty($desc) ? ":" : "") . var_export($data, 1) . "\n", 3, $file);
	} else {
		error_log($desc . (!empty($desc) ? ":" : "") . $data . "\n", 3, $file);
	}
}
function log_file_sql() {
	log_file(db()->getlastsql());
}
// ---
// 钩子
function hook($hook, $params=array()) {
    \think\Hook::listen($hook, $params);
}
// 通过ID获取到图片的缩略图 - 类型 qiniu七牛 local本地 saeSAE
function getThumbImageById($cover_id, $width=100, $height='auto', $type=0, $replace=false) {
    $picture = cache('picture_' . $cover_id);
    if ( empty($picture) ) {
        $picture = db('common_picture')->find($cover_id);
        cache('picture_' . $cover_id, $picture);
    }

    if ( empty($picture) ) return get_pic_src('Public/images/nopic.png');
    if ( $picture['type'] == 'local' ) {
        $attach = getThumbImage("uploads/".$picture['path'], $width, $height, $type, $replace);
        return $attach;
    } else { // 未完成
        $new_img = $picture['path'];
        $name = get_addon_class($picture['type']);
        if ( class_exists($name) ) {
            $class = new $name();
            if ( method_exists($class, 'thumb') ) $new_img = $class->thumb($picture['path'], $width, $height, $type, $replace);
        }
        return get_pic_src($new_img);
    }
}
function get_pic_src($path) { // 获取图片地址
    $not_http_remote = (strpos($path, 'http://') === false);
    $not_https_remote = (strpos($path, 'https://') === false);
    if ( $not_http_remote && $not_https_remote ) {
        return str_replace('//', '/',  $path);
    } else {
        return $path;
    }
}
function getThumbImage($image_path, $width, $height) {
	$file_name = $_SERVER['DOCUMENT_ROOT']."/uploads/thumb/".md5("thumb".$image_path.$width.$height).".jpg";

	$image = \think\Image::open($image_path);
	$image->thumb($width, $height)->save($file_name);

	return $file_name;
}
function thumb($cover_id, $width=100, $height='auto', $type=0, $replace=false) { // 缩略图
    return getThumbImageById($cover_id, $width, $height, $type, $replace);
}
function modconfig($key, $default='', $module='') { // 模块配置
    $mod = $module ? $module : $request->module();
    $tag = 'conf_' . strtoupper($mod).'_'.strtoupper($key);
    $result = cache($tag);

    if ( $result === false ) {
        $config = db('config')->field('value')->where(array('name' => '_'.strtoupper($mod).'_'.strtoupper($key)))->find();
        if ( !$config ) {
            $result = $default;
        } else {
            $result = $config['value'];
        }
        cache($tag, $result);
    }

    return $result;
}
function get_upload_config($driver) { // 获取 上传配置
    if ( $driver == 'local' ) { // 完成
        $uploadConfig = config("upload_{$driver}");
    } else {
        $name = get_addon_class($driver);
        $class = new $name();
        $uploadConfig = $class->uploadConfig();
    }

    return $uploadConfig;
}
function get_addon_class($name) { // 获取 插件类
    return "addons\\{$name}\\{$name}";
}